import java.io.*;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : Swicky
 * @date : 2021/6/16 - 9:53 上午
 */
public class ChangePackagePathUtil {

    static String projectBasePath = "/Users/jlgl/IdeaProjects/";
    static String projectName = "base-admin";
    static String newProjectName = "ggr-common-config";
    static String packageFilePath = "cn/huanzi/qch/baseadmin";
    static String newPackageFilePath = "com/shaoyu/common/config";

    static String packagePath = packageFilePath.replaceAll("[/]",".");
    static String newPackagePath = newPackageFilePath.replaceAll("[/]",".");

    static String mavenGroupId = "cn.huanzi.qch";
    static String newMavenGroupId = "com.shaoyu.common";
    static String artifactId = "base-admin";
    static String newArtifactId = newProjectName;
    static List<String> ignoreFiles = new ArrayList<String>(){
        {
            this.add("target");
            this.add(".idea");
            this.add("test");
            this.add(".DS_Store");
            this.add("LICENSE");
            this.add("README.md");
            this.add("base-admin.iml");
            this.add(".git");
            this.add(".gitattributes");
        }
    };
    //这里的文件不能用 readLine 复制
    static String doNotNeedToReplaces = "resources/static/common";


    public static void main(String[] args) {
        moveFile();
        File projectFile = new File(projectBasePath+newProjectName);
        listFile(projectFile,0);
    }

    //树形展示文件
    private static void listFile(File file,int index) {
        if(ignoreFiles.contains(file.getName()))return;
        if(!file.exists()){
            System.out.println("no file fond!");
            return;
        }
        for (int i = 0; i < index; i++) {
            System.out.print("\t");
        }
        System.out.println("|-"+file.getName());
        if(file.isDirectory()){
            for (File listFile : file.listFiles()) {
                listFile(listFile,index+1);
            }
        }
    }
    private static void copyFileReadLine(File oldFile,String newFileName){
        BufferedReader reader = null;
        BufferedWriter writer = null;
        try {
            reader = new BufferedReader(new FileReader(oldFile));
            writer = new BufferedWriter(new FileWriter(new File(newFileName)));
            String s = reader.readLine();
//            System.out.println(newFileName);
            while (s!=null) {
                s = s.replaceAll(packagePath, newPackagePath);
                s = s.replaceAll(mavenGroupId, newMavenGroupId);
                s = s.replaceAll(artifactId, newArtifactId);
                writer.write(s);
                writer.newLine();
//                System.out.println(s);
                s = reader.readLine();
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(reader != null)reader.close();
                if(writer != null)writer.close();
            }catch (Exception e){

            }
        }
    }
    private static void copyFile(File oldFile,String newFileName){
        if(!newFileName.contains(doNotNeedToReplaces)){
            copyFileReadLine(oldFile, newFileName);
            return;
        }
        FileChannel input = null;
        FileChannel output = null;
        try {
            input = new FileInputStream(oldFile).getChannel();
            output = new FileOutputStream(new File(newFileName)).getChannel();
            output.transferFrom(input, 0, input.size());
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try{
                if(input != null)input.close();
                if(output != null)output.close();
            }catch (Exception e) {

            }
        }
    }

    private static File moveFile() {
        File oldFile = new File(projectBasePath+projectName);
        File newProject = new File(projectBasePath+newProjectName);
        String [] baseDirs = {"/src/main/java","/src/main/resources"};
        //新建基础文件夹
        for (String baseDir : baseDirs) {
            new File(projectBasePath+newProjectName+baseDir).mkdirs();
        }
        //复制项目根目录下的文件
        File[] files = oldFile.listFiles();
        for (File file : files) {
            if(file.isFile()){
                copyFile(file,projectBasePath+newProjectName+"/"+file.getName());
            }
        }
        //复制 java 文件
        String javaFileBasePath = projectBasePath+projectName+"/src/main/java/"+packageFilePath;
        String javaNewFileBasePath = projectBasePath+newProjectName+"/src/main/java/"+newPackageFilePath;
        File javaFileBaseFile = new File(javaFileBasePath);
        for (File file : javaFileBaseFile.listFiles()) {
            if(file.isDirectory()){
                copyDir(file,javaNewFileBasePath);
            }else {
                copyFile(file, javaNewFileBasePath+"/"+file.getName());
            }
        }
        //复制 resource
        String resourceFileBasePath = projectBasePath+projectName+"/src/main/resources";
        String resourceNewFileBasePath = projectBasePath+newProjectName+"/src/main/resources";
        File resourceFileBaseFile = new File(resourceFileBasePath);
        for (File file : resourceFileBaseFile.listFiles()) {
            if(file.isDirectory()){
                copyDir(file,resourceNewFileBasePath);
            }else {
                copyFile(file, resourceNewFileBasePath+"/"+file.getName());
            }
        }
        return newProject;
    }

    private static void copyDir(File file, String javaNewFileBasePath) {
        //新建目录
        String newFileName = javaNewFileBasePath+"/"+file.getName();
        if(file.isDirectory()){
            File newDir = new File(newFileName);
            newDir.mkdirs();
            for (File listFile : file.listFiles()) {
                copyDir(listFile,newFileName);
            }
        }else{
            copyFile(file,newFileName);
        }
    }
}
